package com.android.alarmbuy;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

import com.android.alarmbuy.service.AlarmService;
import com.android.alarmbuy.utils.SharePreferenceUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    WebView mWebView;
    ImageView mAlarm;
    Button mRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUI();
        initWebView();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.android.alarmbuy");
        registerReceiver(receiver, filter);
        startService(new Intent(this, AlarmService.class));
    }

    private void initUI() {
        mWebView = findViewById(R.id.web_view);
        mAlarm = findViewById(R.id.alarm);
        mAlarm.setOnClickListener(this);
        mRefresh = findViewById(R.id.refresh);
        mRefresh.setOnClickListener(this);
    }

    private void initWebView() {
        if (mWebView != null) {
            WebSettings settings = mWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            //支持通过JS打开新窗口
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setSupportZoom(true);
            settings.setTextZoom(100);
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            settings.setBlockNetworkImage(false);
            settings.setDomStorageEnabled(true);
            settings.setDatabaseEnabled(true);
//        settings.setAppCacheMaxSize(1024*1024*8);
            String appCachePath = getCacheDir().getAbsolutePath();
            settings.setAppCachePath(appCachePath);
            settings.setAllowFileAccess(true);
            settings.setAppCacheEnabled(false);

            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    //Log.e(App.TAG, "shouldOverrideUrlLoading");
                    mWebView.loadUrl(request.getUrl().toString());
                    return true;
                }
            });
            mWebView.loadUrl("http://zx.maichaye.cn/list.php");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(AlarmApplication.response.equals("0")) {
            stopAlarm();
        } else if (AlarmApplication.response.equals("1")) {
            doAlarm();
        }
    }

    public void doAlarm() {
        Log.d("leihuiming", "doAlarm");
        mWebView.reload();
        //AlarmApplication.playSound(this);
        mAlarm.setClickable(true);
        //mAlarm.setBackgroundColor(getColor(android.R.color.holo_green_light));
        mAlarm.setImageResource(R.mipmap.pause);
    }

    public void stopAlarm() {
        Log.d("leihuiming", "stopAlarm");
        AlarmApplication.stopSound();
        mAlarm.setClickable(false);
        //mAlarm.setBackgroundColor(getColor(android.R.color.darker_gray));
        mAlarm.setImageResource(R.mipmap.play);
    }

    public String readStatus() {
        String result =  SharePreferenceUtil.getString(this, "alarm", "0");
        Log.d("leihuiming", "readStatus result = " + result);
        return result;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.refresh) {
            Log.d("leihuiming", "click refresh");
            mWebView.clearCache(true);
            mWebView.reload();
        } else if (v.getId() == R.id.alarm) {
            Log.d("leihuiming", "click alarm");
            stopAlarm();
        }
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if("com.android.alarmbuy".equals(action)) {
                doAlarm();
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("leihuiming", "onDestroy");
        unregisterReceiver(receiver);
    }
}
